<template>
	<view class="container">
		<uni-nav-bar class="ch-nav" height="88rpx" backgroundColor="#ffffff" leftWidth="90rpx" rightWidth="90rpx" color="#333" :fixed="true" :padding="false" :border="false" @clickLeft="$goBack(1)">
			<template slot="left">
				<image class="left" src="@/public/images/public/dhl_fhjt.png" mode="aspectFit"></image>
			</template>
			<text class="title">浏览记录</text>
		</uni-nav-bar>
		<view class="head flex-r ac" :class="navBorder ? 'navborder':''">
			<template v-for="(item,index) in tabList">
				<view class="tab-list flex-r ac" :class="{'active': tabCur == index}" :key="index" @tap="tabChange(index)">
					<image src="@/public/images/public/fgx.png" mode="aspectFill" v-if="index != 0"></image>
					<text class="text-regular">{{item.name}}</text>
				</view>
			</template>
		</view>
		<template v-for="(item,index) in tabList">
		<keep-alive>
			<view class="body bottom-ios" @touchstart="touchStart" @touchend="touchEnd" v-if="tabCur == index" :key="index">
				<EmptyContent v-if="!userReadLog[tabCur][0]" top="250rpx" text="暂无浏览记录" img="qsy_zwxl" @refresh="getUserRaedLog"></EmptyContent>
				<ArticleList v-else-if="tabCur == 0" :list="userReadLog[tabCur]" type="readlog" :showBar="true" :showResult="true" :marginTop="false" :showSP="false" :remark='true'></ArticleList>
				<ContentList v-else :marginTop="false" :list="userReadLog[tabCur]" :radius="true" type="readlog"></ContentList>
				<view class="list-end flex-r pb20" v-if="tabData[tabCur].isEnd && userReadLog[tabCur][0]">
					<view class="list-end-line"></view>
					<text>加载完成，以上为您近7天内的浏览内容</text>
					<view class="list-end-line"></view>
				</view>
			</view>
		</keep-alive>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userReadLog: [[],[]],
				
				tabList: [
					{name: '方案'},{name: '晒单'}
					// {name: '方案'},{name: '晒单'}
				],
				tabCur: 0,
				tabData: [
					{pageNo: 1,	isEnd: false},
					{pageNo: 1,	isEnd: false},
				],
				
				navBorder: false
			}
		},
		components: {
			ContentList: () => import("@/components/ch-article/ch-contentlist.vue") //讨论贴列表
		},
		onLoad() {
			this.getUserRaedLog()
		},
		onReachBottom() {
			if(!this.tabData[this.tabCur].isEnd){
				this.getUserRaedLog()
			}
		},
		onPageScroll(e) {
			if(e.scrollTop > 10){
				this.navBorder = true
			}else{
				this.navBorder = false
			}
		},
		methods: {
			//获取浏览记录
			getUserRaedLog() {
				this.$http.get({
					url: '/api/forum/article/browse/records',
					data: {
						pageNo: this.tabData[this.tabCur].pageNo,
						pageSize: 10,
						type: this.tabCur + 1==1?this.tabCur + 1:3
					},
				}).then(data => {
					console.log(data,'浏览记录');
					if(data.success && data.result && data.result.total){
						if(data.result.current == 1){
							this.$set(this.userReadLog, this.tabCur, data.result.records)
						}else{
							this.userReadLog[this.tabCur].push(...data.result.records)
						}
						this.tabData[this.tabCur].isEnd = data.result.current >= data.result.pages
						this.tabData[this.tabCur].pageNo = data.result.current + 1
					}
				}).catch((err) => {
					console.log(err);
				})
			},
			//进入详情
			goArticleDetail(id, index) {
				uni.$once('readNum', (data)=>{
					this.userReadLog[index].virtualReadNum = data.num
				})
				this.$CHS.goArticleDetail(id)
			},
			//选项卡切换
			tabChange(index) {
				this.tabCur = index;
				if(!this.userReadLog[index][0]){
					this.getUserRaedLog()
				}
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			},
			//触摸滑动开始
			touchStart(e) {
				if (e.changedTouches.length == 1) {
					this.startX = e.changedTouches[0].clientX
					this.startY = e.changedTouches[0].clientY
				}
			},
			//触摸滑动结束
			touchEnd(e) {
				if (e.changedTouches.length == 1) {
					var endX = e.changedTouches[0].clientX;
					var endY = e.changedTouches[0].clientY;
					var xTowards = endX - this.startX;
					var yTowards = endY - this.startY;
					if (xTowards > 50 && Math.abs(yTowards) < 50) {//向右
						console.log('向右')
						if(this.tabCur > 0){
							this.tabChange(this.tabCur - 1)
						}
					}
					if (xTowards < -50 && Math.abs(yTowards) < 50) {//向左
						console.log('向左')
						let maxlen = this.tabData.length - 1;
						if(this.tabCur < maxlen){
							this.tabChange(this.tabCur + 1)
						}
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.head{
		max-width: $max-width;
		margin: $pc-margin;
		width: 100%;
		position: fixed;
		background-color: #FFFFFF;
		z-index: 6;
		.tab-list{
			flex: 2;
			height: 88rpx;
			image{
				width: 1rpx;
				height: 32rpx;
			}
			.text-regular{
				flex: 2;
				text-align: center;
			}
			&.active{
				color: #0055FF;
				font-weight: 500;
				background-image: url("~@/public/images/public/zjdl_hk.png");
				background-repeat: no-repeat;
				background-position: bottom center;
				background-size: 60rpx 8rpx;
			}
		}
	}
	.body{
		min-height: calc(var(--safe-height) - #{$navbar-height} - 88rpx - 20rpx);
		margin-top: 88rpx;
		padding: 20rpx;
	}
</style>